/*
 * stepper.c
 *
 * Created: 11/5/2013 7:14:27 PM
 *  Author: Tim
 */ 

#include "../src/Epiphany.h"

void stepMotor(uint8_t direction,uint16_t millis);

void stepperInit();

void partI();
void partII();

int8_t stepperState = 0;

void setup()
{
	//put all your initializations here
	clockInit();
	RTC_DelayInit();
	stepperInit();
}

void loop()
{
	
//	partI();
//	partII();
	
}

void partI()
{
	//step the motor so that is rotates at 2 revolutions per second (120rpm)
}

void partII()
{
	//step the motor 200 steps in alternating directions (1 rpm)
}

void stepMotor(uint8_t direction,uint16_t millis)
{
	
	if(direction)
	{
		if(++stepperState>3) stepperState = 0;
	}
	else
	{
		if(--stepperState<0) stepperState = 3;
	}
	
	switch(stepperState)
	{
		case(0):
		PORTD.OUT = 0x3;
		break;
		case(1):
		PORTD.OUT = 0x6;
		break;
		case(2):
		PORTD.OUT = 0xC;
		break;
		case(3):
		PORTD.OUT = 0x9;
		break;
	}
	RTC_Delay_ms(millis);
	while(delayNotOver);
}

stepperInit()
{
	//set the direction for the stepper pins to be outputs
	PORTD.DIRSET = 0x0F;
	//start the stepper motor out in state 0
	PORTD.OUT = 0x3;
	//best practice to set the stepperState variable here.
	stepperState = 0;
}